<html>
<TITLE>CP2377 Assignment 2 Sample Code Overview</TITLE>

<p>

<h2>CP2377 Assignment 2 Sample Code Overview</h2>

<ul>

<li> Run program <a href="a2_sample1.php">a2_sample1.php</a> and then read
its <a href="a2_sample1.php.html">source code</a>.  <p> This program prints
a <em>Hello World</em> and some other messages, before running the
<em>date</em> command.  Welcome to the start of the PHP lab that introduces
assignment 2.

<p><li> We now retrieve some data from the media web site and pass it
through to the browser.  Run program <a
href="a2_sample2.php">a2_sample2.php</a> and here is its <a
href="a2_sample2.php.html">source code</a>.  <p> This program displays the
current date and time to allow us to confirm web page updates.  Comments
briefly explain the operation of PHP statements to import the HttpClient
code, connect to the remote server, and retrieve and display data.

<p>

<table bgcolor="#ADD8E6"><tr>
<td> &nbsp; &nbsp; &nbsp; &nbsp;</td>
<td> Remember that we are treating <em> HttpClient </em> as a black box that
provides a web connect service.  All subsequent program examples contain
the standard sequence of statements from <em>
require('HttpClient.class.php') </em> to <em> $pageContents =
$client->getContent() </em> inclusive. </td>
</tr></table>

<p><li> Run program <a href="a2_sample3.php">a2_sample3.php</a> to see the
same data displayed, followed by a dump of the raw HTML.  Here is the
corresponding <a href="a2_sample3.php.html">source code</a> with a
<strong>str_replace</strong> modified the web page content so that its
print command outputs plain text.  <p> Before continuing, explain to
yourself how the <em>str_replace</em> works.

<table bgcolor="#ADD8E6"><tr>
<td> &nbsp; &nbsp; &nbsp; &nbsp;</td>
<td> At this point, we can pause and reflect on progress so far.  We
have retrieved the desired data for one news item.  We still need to
extract the actual news item. </td>
</tr></table>

<p><li> We now attempt to extract the actual news item itself (the
information that we saw between the HTML comments earlier).  Read the
source code <a href="a2_sample4.php.html">a2_sample4.php</a> for a first
attempt at extraction and display.  <p> When you understand how this
attempt is designed, try it by running <a
href="a2_sample4.php">a2_sample4.php</a>.  After you see it fail, move onto
the improved version.

<p><li> Run program <a href="a2_sample5.php">a2_sample5.php</a> to see a
dump of the desired "raw" HTML.  Here is the corresponding <a
href="a2_sample5.php.html">source code</a> where you see a small discussion
of RE options.  <p> Finally, run program <a
href="a2_sample6.php">a2_sample6.php</a> to display the content for the
news item (<a href="a2_sample6.php.html">source code</a>).

<p><li> Notes on <strong>preg_match_all</strong>
<ul>

<li> The general form of the control string used by
<strong>preg_match_all</strong> is <br>

<table bgcolor="#ADD8E6"><tr><td>
 <strong>RE_delimiter_char </strong> <em>pattern delimiter1</em> <strong>target pattern</strong> <em>pattern delimiter2</em> <strong>RE_delimiter_char</strong>
</td></tr></table>

<li> The actual control string successfully used here had options "is" i.e.
<br>

<table bgcolor="#ADD8E6"><tr><td>
:<em>&lt;!-- start content --></em><strong>(.*)</strong><em>&lt;!-- end content --></em>:is
</td></tr></table>

<p><li> These examples show <em>preg_match_all()</em> extracting HTML data
of interest from a relatively simple web page.  Because it returns multiple
matches as extra elements in the result arrays, it is also very capable of
processing complete HTML tables and extracting the items into arrays for
easy access.  For further information, see the on-line resources.

</ul>

</ul>

</html>

